package com.leo.blog.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dao.BlogDao;
import com.leo.blog.dao.BlogTypeDao;
import com.leo.blog.dto.BlogTypeExecution;
import com.leo.blog.entity.BlogType;
import com.leo.blog.enums.BlogTypeStateEnum;
import com.leo.blog.exceptions.BlogTypeOperationException;
import com.leo.blog.redis.JedisUtil;
import com.leo.blog.service.BlogTypeService;
import com.leo.blog.service.CacheService;
import com.leo.blog.util.ConstantUtil;

@Service//声明业务逻辑层
public class BlogTypeServiceImpl implements BlogTypeService{
	//注入dao层
	@Autowired
	private BlogTypeDao blogTypeDao;
	@Autowired
	private BlogDao blogDao;
	
	@Autowired
	private CacheService cacheService;
	@Autowired
	private BlogTypeService blogTypeService;
	
	/**
	 * redis
	 */
	@Autowired
	private JedisUtil.Keys jedisKeys;
	@Autowired
	private JedisUtil.Strings jedisString;
	
	private static Logger logger = LoggerFactory.getLogger(BlogTypeServiceImpl.class);
	
	@Override
	@Transactional
	public List<BlogType> getBlogTypeList() {
		//定义redis的key
		String key = BLOGTYPELISTKEY;
		//定义接收对象
		List<BlogType> blogTypeList = null;
		
		// 定义jackson数据转换操作类
		ObjectMapper mapper = new ObjectMapper();
		
		//判断key是否存在
		if(!jedisKeys.exists(key)) {
			//若不存在,则从数据库里面取出相应数据
			blogTypeList = blogTypeDao.queryBlogTypeList();
			//将相关的实体类集合转换成String,存入redis里面对应的key中
			String jsonString;
			try {
				jsonString = mapper.writeValueAsString(blogTypeList);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
				logger.error(e.getMessage());
				throw new BlogTypeOperationException(e.getMessage());
			}
			jedisString.set(key, jsonString);
		}else {
			//若存在,则直接从redis里面取出相应数据
			String jsonString = jedisString.get(key);
			//指定要将String转换成的集合类型
			JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, BlogType.class);
			try {
				//将相关key对应的value里的String转换成对象的实体类集合
				blogTypeList = mapper.readValue(jsonString, javaType);
			} catch (JsonParseException e) {
				e.printStackTrace();
				logger.error(e.getMessage());
				throw new BlogTypeOperationException(e.getMessage());
			}catch (JsonMappingException e) {
				e.printStackTrace();
				logger.error(e.getMessage());
				throw new BlogTypeOperationException(e.getMessage());
			}catch (IOException e) {
				e.printStackTrace();
				logger.error(e.getMessage());
				throw new BlogTypeOperationException(e.getMessage());
			}
		}	
		return blogTypeList;
	}

	@Override
	public BlogTypeExecution getBlogTypeById(int blogTypeId) {
		BlogType blogType = blogTypeDao.queryBlogTypeById(blogTypeId);
		if(blogType != null) {
			return new BlogTypeExecution(BlogTypeStateEnum.SUCCESS, blogType);
		}else {
			return new BlogTypeExecution(BlogTypeStateEnum.NULL_BLOGTYPE);
		}
	}

	@Override
	@SystemLog(description = ConstantUtil.BLOGTYPE_UPDATE, userType = ConstantUtil.USERTYPE_ADMIN)
	public int editBlogType(String name, BlogType blogType) {
		//清除redis的key
		cacheService.removeFromCache(blogTypeService.BLOGTYPELISTKEY);
		return blogTypeDao.updateBlogType(blogType);
	}

	@Override
	@SystemLog(description = ConstantUtil.BLOGTYPE_DELETE, userType = ConstantUtil.USERTYPE_ADMIN)
	public int deleteBlogType(String name, int blogTypeId) {
		//清除redis的key
		cacheService.removeFromCache(blogTypeService.BLOGTYPELISTKEY);
		return blogTypeDao.deleteBlogType(blogTypeId);
	}

	@Override
	@SystemLog(description = ConstantUtil.BLOGTYPE_ADD, userType = ConstantUtil.USERTYPE_ADMIN)
	public int addBlogType(String name, BlogType blogType) {
		//赋初始值
		blogType.setBlogTypeNum(0);
		blogType.setCreateTime(new Date());
		//清除redis的key
		cacheService.removeFromCache(blogTypeService.BLOGTYPELISTKEY);
		
		return blogTypeDao.insertBlogType(blogType);
	}

	@Override
	public int getBlogByBlogTypeCount(int blogTypeId) {
		return blogDao.queryBlogByBlogTypeCount(blogTypeId);
	}

}
